package com.siemens.industrial.web.mvc.service.admin;

import com.siemens.industrial.model.admin.TenantServiceRelation;
import com.siemens.industrial.web.mvc.dao.admin.TenantServiceRelationDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by qupen on 2016/9/23.
 */
@Service
public class TenantServiceRelationService {
    @Resource
    TenantServiceRelationDao tenantServiceRelationDao;

    public void saveTenantService(Map<String, Object> o) {
//        TenantServiceRelation tenantServiceRelation = new TenantServiceRelation();
//        if (o.containsKey("tenantId") && o.containsKey("tenantId") && o.containsKey("tenantId")) {
//            tenantServiceRelation.setTenantId(o.get("tenantId").toString());
//            tenantServiceRelation.setServiceId(o.get("tenantId").toString());
//            tenantServiceRelation.setStartDate(o.get("tenantId").toString());
//        }
//        if (o.containsKey("")) {
//            tenantServiceRelation.setEndDate(o.get("").toString());
//        }
//        if (o.containsKey("")) {
//            tenantServiceRelation.setEnable(Integer.parseInt(o.get("").toString()));
//        }
//    tenantServiceRelationDao.save(tenantServiceRelation);
    }

    @Transactional
    public void setTenantService(String tenantId, String[] services, int mouth) {
        Calendar cal = Calendar.getInstance();
        for (String serviceId : services) {
            TenantServiceRelation tenantServiceRelation = this.getTenantAndServiceRelation(tenantId, serviceId);
            if (tenantServiceRelation != null) {
                java.sql.Date endDate = tenantServiceRelation.getEndDate();
                cal.setTime(endDate);
                cal.add(Calendar.MONTH, mouth);
                tenantServiceRelation.setEndDate(new java.sql.Date(cal.getTime().getTime()));

                tenantServiceRelationDao.updateEndDate(tenantServiceRelation);
            } else {
                TenantServiceRelation ts = new TenantServiceRelation();
                Date endDate = new java.sql.Date(new Date().getTime());
                cal.setTime(endDate);
                cal.add(Calendar.MONTH, +mouth);
                ts.setTenantId(tenantId);
                ts.setServiceId(serviceId);
                ts.setStartDate(new java.sql.Date(new Date().getTime()));
                ts.setEndDate(new java.sql.Date(cal.getTime().getTime()));
                ts.setEnabled(1);

                tenantServiceRelationDao.save(ts);
            }
        }
    }

    public int deleteTenantService(String tenantId, String serviceId) {
        return tenantServiceRelationDao.deleteRelation(tenantId, serviceId);
    }

    public TenantServiceRelation getTenantAndServiceRelation(String tenantId, String serviceId) {
        return tenantServiceRelationDao.getOneByTenantIdAndServiceId(tenantId, serviceId);
    }
}
